package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Teacher;

public class TeacherDao  implements BaseTeacherDao {
    private static Teacher[] teachers = new Teacher[5];

    static {
        Teacher t1 = new Teacher("itheima001", "张三", "23", "1997-10-10");
        Teacher t2 = new Teacher("itheima002", "李四", "23", "1997-10-10");
        teachers[0] = t1;
        teachers[1] = t2;
    }

    public boolean addTeacher(Teacher teacher) {
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            if (teachers[i] == null) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            return false;
        } else {
            teachers[index] = teacher;
            return true;
        }
    }

    public Teacher[] findAllTeacher() {
        return teachers;
    }

    public void deleteTeacher(String sid) {
        int index = getIndex(sid);
        teachers[index] = null;
    }

    public void updateTeacher(String sid, Teacher teacher) {
        int index = getIndex(sid);
        teachers[index] = teacher;
    }

    public int getIndex(String sid) {
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher != null && sid.equals(teacher.getSid())) {
                index = i;
            }
        }
        return index;
    }


}
